package cn.wolfcode.shop.web.controller;

import cn.wolfcode.shop.redis.UserKeyPrefix;
import cn.wolfcode.shop.result.CodeMsg;
import cn.wolfcode.shop.service.IUserService;
import cn.wolfcode.shop.web.LoginVo;
import cn.wolfcode.shop.web.result.Result;
import cn.wolfcode.shop.web.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
public class LoginController {
    @Reference
    private IUserService userService;
    @RequestMapping("/doLogin")
    @ResponseBody
    public Result<CodeMsg> doLogin(HttpServletResponse response,@Valid LoginVo loginVo) {
        String token = userService.login(Long.valueOf(loginVo.getMobile()), loginVo.getPassword());
        //将token存进cookie中响应给用户,下次请求携带token 来验证身份
        CookieUtil.addCookie(response, UserKeyPrefix.USER_TOKEN_COOKIE_NAME,token,UserKeyPrefix.USER_TOKEN.getExpireSeconds());
        //登录成功
        return Result.success(token);
    }
}
